﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
 
using Tao;
using Tao.OpenGl;
using Tao.Platform;
using VoltGameEditor.Properties;

namespace VoltGameEditor
{
    public partial class SettingsDiag : DockContentEx
    {
		public TextBox[] Tba;
		public Button[] ba;
		public List<Dictionary<string, Color>> presets = new List<Dictionary<string, Color>>();

        public SettingsDiag()
        {
            InitializeComponent();
			this.docType = Globals.DocType.StartPage;
			Tba = new TextBox[] { tb_bp0, tb_bp1, tb_bp2, tb_bp3, tb_bp4, tb_bp5 };
			ba = new Button[] { b_browse0, b_browse1, b_browse2, b_browse3, b_browse4, b_browse5 };

			//USE THIS FOR RELOCATING ANY FLOATING DOCK CONTENT
			//this.FloatAt(new Rectangle(location.x, location.y, width, height));
        }

        public int plpi = -1; // __ __ Page Index
        public static KeyEventArgs kea;
        public static MouseEventArgs mea;

        private void options_Load(object sender, EventArgs e)
        {
			string game = Globals.Game.Quake_2.ToString().Replace("_", " ");
			label14.Text = game;
			tp_gameCfg.Text = game + " Configuration";

            if (plpi != -1)
            {
                switch (plpi)
                {
                    case 0://goto tab page 1
                        tabControl1.SelectedIndex = 0;
						break;
                    case 1://goto tab page 2
                        tabControl1.SelectedIndex = 1;
						break;
                    case 2://goto tab page 3
                        tabControl1.SelectedIndex = 2;
						break;
                    case 3://goto tab page 4
                        tabControl1.SelectedIndex = 3;
						break;
                    case 4://goto tab page 5
                        tabControl1.SelectedIndex = 4;
                        break;
                    case 5://goto tab page 6
                        tabControl1.SelectedIndex = 5;
						break;
                }
            }
            AppSettings.LoadSettings();
            //cb_presets.SelectedIndex = 0;

        }
        private void options_FormClosed(object sender, FormClosedEventArgs e)
        {
            plpi = -1;
        }

        private void cb_fog_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_fog.Checked == true)
            {
                //Settings.Default.EnableFog = true;
                label2.Enabled = true;
                label3.Enabled = true;
                label4.Enabled = true;
                fog_startDist.Enabled = true;
                fog_density.Enabled = true;
                fog_color.Enabled = true;
            }
            else if (cb_fog.Checked == false)
            {
                //Settings.Default.EnableFog = false;
                label2.Enabled = false;
                label3.Enabled = false;
                label4.Enabled = false;
                fog_startDist.Enabled = false;
                fog_density.Enabled = false;
                fog_color.Enabled = false;
            }
        }

        private void buttons_Click(object sender, EventArgs e)
        {
            functions.AssignKey((Control)sender);
        }

        private void gridColor_Click(object sender, EventArgs e)
        {
            colors.Color = ((Button)sender).BackColor;
            colors.ShowDialog();
            ((Button)sender).BackColor = colors.Color;
        }

        private void b_applyAll_Click(object sender, EventArgs e)
        {
			AppSettings.SaveSettings();
        }

        private void b_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void b_default_Click(object sender, EventArgs e)
        {
			AppSettings.SetDefaults();
		}

		private void b_browse0_Click(object sender, EventArgs e)
		{
			Button b = (Button)sender;
			DialogResult = files_o.ShowDialog();
			if (DialogResult == System.Windows.Forms.DialogResult.OK)
			{
				Tba[int.Parse(b.Name.Remove(0, b.Name.Length - 1))].Text = files_o.FileName;
			}
		}

		private void b_saveTheme_Click(object sender, EventArgs e)
		{
			Dictionary<string, Color> d = new Dictionary<string, Color>();
			string a = Program.ValEditor.ShowValEditor("Name=");
			if (a == null)
				return;
			foreach (string s in cb_presets.Items)
			{
				if (s == a)
				{
					MessageBox.Show("There is already one called that...", "Exclamation");
					return;
				}
			}

			d["Color_Grid="]= gridColor.BackColor;
			d["Color_2dViewBG="] = viewBGColor.BackColor;
			d["Color_3dViewBG="] = viewBGColor3D.BackColor;
			d["Color_BrushEnts="] = brushEntColor.BackColor;
			d["Color_Dups="] = dupColor.BackColor;
			d["Color_ObjectLines="] = objColor.BackColor;
			d["Color_Fog="] = fog_color.BackColor;
			d["Color_SelectedObjects="] = selObjColor.BackColor;
			d["Color_Fog="] = fog_color.BackColor;

			cb_presets.Items.Add(a);
			cb_presets.SelectedIndex = cb_presets.Items.IndexOf(a);
			presets.Add(d);
		}

		private void cb_presets_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (presets.Count == 0)
				return;
			int i = cb_presets.SelectedIndex;
			try
			{
				foreach (KeyValuePair<string, Color> kvp in presets[i])
				{
					switch (kvp.Key)
					{
						case "Color_Grid":
							gridColor.BackColor = kvp.Value;
							break;
						case "Color_2dViewBG":
							viewBGColor.BackColor = kvp.Value;
							break;
						case "Color_3dViewBG":
							viewBGColor3D.BackColor = kvp.Value;
							break;
						case "Color_BrushEnts":
							brushEntColor.BackColor = kvp.Value;
							break;
						case "Color_Dups":
							dupColor.BackColor = kvp.Value;
							break;
						case "Color_ObjectLines":
							objColor.BackColor = kvp.Value;
							break;
						case "Color_SelectedObjects":
							selObjColor.BackColor = kvp.Value;
							break;
						case "Color_Fog":
							fog_color.BackColor = kvp.Value;
							break;
					}
				}
			}
			catch { }
			
		}

		private void SettingsDiag_FormClosing(object sender, FormClosingEventArgs e)
		{
			e.Cancel = true;
			this.Hide();
		}
    }
}